package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.BomInfoTmpService;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
public class BomInfoTmpServiceImpl extends BaseServiceImpl<BomInfoTmp> implements BomInfoTmpService {

    @Transactional(
            readOnly = false
    )
    @Override
    public BomInfoTmp listBomTmp(String itemCode){
        List<Item> itemList = JpaUtil.linq(Item.class).equal("itemCode",itemCode).equal("itemSource","1").list();
        if(CollectionUtils.isEmpty(itemList)){
            return new BomInfoTmp();
        }
        Item item = itemList.get(0);
        List<BomInfoTmp> tmpList = JpaUtil.linq(BomInfoTmp.class).equal("parentCode",itemCode).list();
        BomInfoTmp bomInfoTmp;
        if(CollectionUtils.isEmpty(tmpList)){
            bomInfoTmp = new BomInfoTmp();
            bomInfoTmp.setParentCode(item.getItemCode());
            bomInfoTmp.setParentName(item.getItemName());
            bomInfoTmp.setParentGroup(item.getItemGroupCode());
            bomInfoTmp.setParentSpec(item.getItemSpec());
            bomInfoTmp.setParentUnit(item.getUnitCode());
        }
        else{
            bomInfoTmp = tmpList.get(0);
            List<BomInfoLineTmp> lineTmpList = JpaUtil.linq(BomInfoLineTmp.class).equal("bomId",bomInfoTmp.getBomId()).list();
            bomInfoTmp.setBomInfoLineList(lineTmpList);
        }
        return bomInfoTmp;
    }
    @Override
    @Transactional(
            readOnly = false
    )
    public Page<BomInfoTmp> page(Pageable page, Criteria c){
        Page<Item> itemPage = JpaUtil.linq(Item.class).where(c).equal("itemSource","1").paging(page);
        Long count = JpaUtil.linq(Item.class).where(c).equal("itemSource","1").count();
        List<Item> itemList = itemPage.getContent();
        Set<String> itemCodes = JpaUtil.collect(itemList, "itemCode");
        List<BomInfoTmp> bomInfoList= JpaUtil.linq(BomInfoTmp.class).in("parentCode",itemCodes).list();
        Map<String,BomInfoTmp> bomMap = JpaUtil.index(bomInfoList,"parentCode");
        List<BomInfoTmp> returnList = new ArrayList<>();
        for(Item item:itemList){
            BomInfoTmp bomInfo = bomMap.get(item.getItemCode());
            if(Objects.isNull(bomInfo)){
                bomInfo = new BomInfoTmp();
            }
            bomInfo.setParentCode(item.getItemCode());
            bomInfo.setParentName(item.getItemName());
            bomInfo.setParentGroup(item.getItemGroupCode());
            bomInfo.setParentSpec(item.getItemSpec());
            bomInfo.setParentUnit(item.getUnitCode());
            bomInfo.setParentDrawingNo(item.getDrawingNo());
            returnList.add(bomInfo);
        }
        return new PageImpl<>(returnList, page, count);
    }

    @Transactional(
            readOnly = false
    )
    @Override
    public Result<BomInfoTmp> updateBomStatus(BomInfoTmp bomInfo, OrderStatusEnums.BomStatus bomStatus){
        bomInfo.setUseType(bomStatus.getStatusCode());
        this.update(bomInfo);
        return Result.OK();
    }

    @Transactional(
            readOnly = false
    )
    @Override
    public boolean audit(String bomId){
        try{
            BomInfoTmp bomInfoTmp = getById(BomInfoTmp.class,bomId);
            List<BomInfoLineTmp> tmpList = JpaUtil.linq(BomInfoLineTmp.class).equal("bomId",bomId).list();
            BomInfoAudit bomInfoAudit = new BomInfoAudit();
            BeanReflectionUtils.copyProperties(bomInfoAudit,bomInfoTmp);
            bomInfoAudit.setChangeCode(null);//清空变更单单号
            bomInfoAudit.setUseType(OrderStatusEnums.BomStatus.AUDITING.getStatusCode());
            JpaUtil.save(bomInfoAudit);
            for(BomInfoLineTmp o:tmpList){
                BomInfoLineAudit audit = new BomInfoLineAudit();
                BeanReflectionUtils.copyProperties(audit,o);
                JpaUtil.save(audit);
            }
            bomInfoTmp.setUseType(OrderStatusEnums.BomStatus.USING.getStatusCode());
            JpaUtil.save(bomInfoTmp);
            return true;
        }
        catch(Exception e){
            log.error(e.getMessage(),e);
        }
        return false;
    }

    @Transactional(
            readOnly = false
    )
    @Override
    public boolean validBeginTime(BomInfoTmp bomInfoTmp){
        List<BomInfo> bomInfos = JpaUtil.linq(BomInfo.class).equal("parentCode",bomInfoTmp.getParentCode()).equal("primaryBom","1").list();
        if(CollectionUtils.isEmpty(bomInfos)){
            return true;
        }
        for(BomInfo bomInfo:bomInfos){
            if(bomInfo.getBeginTime().compareTo(bomInfoTmp.getBeginTime())>=0){
                return false;
            }
        }
        return true;
    }


}
